{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# Notebook Disposable\n",
    "\n",
    "This notebook presents how to use the `notebookDisposable` notebook variable for disposing elements i.e. when restarting the kernel."
   ]
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "%use intellij-platform",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import com.intellij.icons.AllIcons\n",
    "import com.intellij.openapi.application.runInEdt\n",
    "import com.intellij.openapi.util.Disposer\n",
    "import com.intellij.openapi.wm.ToolWindowManager\n",
    "\n",
    "val project = currentProject() ?: error(\"Project not found\")\n",
    "val toolWindowManager = ToolWindowManager.getInstance(project)\n",
    "\n",
    "runInEdt {\n",
    "    val toolWindow = toolWindowManager.getToolWindow(\"My Tool Window\")\n",
    "        ?: toolWindowManager.registerToolWindow(\"My Tool Window\") {\n",
    "            icon = AllIcons.General.Error\n",
    "        }\n",
    "\n",
    "    Disposer.register(notebookDisposable) {\n",
    "        toolWindow.remove()\n",
    "    }\n",
    "}"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "After running the cell above, you should see a tool window with the error icon in the tool window strip.\n",
    "When you restart the kernel, the tool window will be removed as well."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Kotlin",
   "language": "kotlin",
   "name": "kotlin"
  },
  "language_info": {
   "name": "kotlin",
   "version": "1.9.23",
   "mimetype": "text/x-kotlin",
   "file_extension": ".kt",
   "pygments_lexer": "kotlin",
   "codemirror_mode": "text/x-kotlin",
   "nbconvert_exporter": ""
  },
  "ktnbPluginMetadata": {
   "sessionRunMode": "IDE_PROCESS"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
